import javax.swing.tree.TreeNode;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 邓大帅
 * Date: 2023-02-01
 * Time: 15:49
 */
public class Tree {
        //反转树
        public TreeNode invertTree(TreeNode root) {
            if(root == null || (root.left == null && root.right == null)) {
                return root;
            }
            TreeNode tmp = root.right;
            root.right = root.left;
            root.left = tmp;
            invertTree(root.left);
            invertTree(root.right);
            return root;
        }
        //判断是否为对称树
        public boolean isSymmetric(TreeNode root) {
            return isSymmetric1(root.left,root.right);
        }
        public boolean isSymmetric1(TreeNode left, TreeNode right) {
            if(right == null && left == null) {
                return true;
            }
            if(left == null || right == null) {
                return false;
            }
            if(right.val != left.val) {
                return false;
            }
            return isSymmetric1(left.left,right.right) && isSymmetric1(left.right,right.left);
        }
        //判断是否为相同的树
        public boolean isSameTree(TreeNode p, TreeNode q) {
            if(p == null && q == null) {
                return true;
            }
            if(p == null || q == null) {
                return false;
            }
            if(p.val != q.val) {
                return false;
            }
            return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
        }

}
